<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 
    同源：
      协议、端口号、域名不许相同 
    -->
    <!-- 用后端响应页面打开实现同源 -->
    <h1>跨域</h1>
    <button>get</button>
    <script>
      let btn = document.querySelector('button')
      btn.onclick = function () {
        var xhr = new XMLHttpRequest()
        xhr.open('get', '/data')
        xhr.send()
        xhr.onreadystatechange = function () {
          if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
              console.log(xhr.response)
            }
          }
        }
      }
    </script>
  </body>
</html>
